package com.xzssmk.leisure.x.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.xzssmk.leisure.x.po.StaffMerchant;
import com.xzssmk.leisure.x.service.StaffMerchantService;
import com.xzssmk.leisure.x.util.DataTables;
import com.xzssmk.leisure.x.util.RequestStatusUtil;
import com.xzssmk.leisure.x.util.SMSUtil;
import com.xzssmk.leisure.x.vo.StaffMerchantVo;

@RestController
@RequestMapping("/staff")
public class StaffMerchantController {

    @Autowired
    private StaffMerchantService staffMerchantService;

    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String add(StaffMerchantVo staffMerchantVo) {
        return JSONObject.toJSONString(staffMerchantService.insert(staffMerchantVo));
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String updateRole(@ModelAttribute StaffMerchantVo staffMerchantVo) {
        return JSONObject.toJSONString(staffMerchantService.update(staffMerchantVo));
    }

    @RequestMapping(value = "/getStaff", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String getById(Integer id) {
        return JSONObject.toJSONString(staffMerchantService.selectOne(id));
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public Object delete(@RequestParam("idlist[]") List<Integer> idlist) {
        return JSONObject.toJSONString(staffMerchantService.delete(idlist));
    }

    @RequestMapping(value = "/sendMsg", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public Object sendMsg(Integer id) {
        Map<String, Object> map = new HashMap<>();
        StaffMerchant staffMerchant = staffMerchantService.selectOne(id);
        if (staffMerchant != null) {
            if (SMSUtil.sendMsg(staffMerchant.getInvitePhone(), staffMerchant.getInvitCode())) {
                map.put("status", RequestStatusUtil.SUCCESS);
            } else {
                map.put("status", RequestStatusUtil.FAILURE);
            }
        } else {
            map.put("status", RequestStatusUtil.FAILURE);
        }
        return JSONObject.toJSONString(map);
    }

    @RequestMapping(value = "/getPageList", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String getPageList(HttpServletRequest request) {
        return JSONObject.toJSONString(staffMerchantService.getPageList(DataTables.getInstance(request, null)));
    }

    @RequestMapping(value = "/getScenicSpotSelect", method = RequestMethod.POST,
        produces = "application/json; charset=utf-8")
    public String getScenicSpotSelect() {
        return JSONObject.toJSONString(staffMerchantService.getScenicSpotSelect());
    }

    @RequestMapping(value = "/getStoreLargessSelect", method = RequestMethod.POST,
        produces = "application/json; charset=utf-8")
    public String getStoreLargessSelect() {
        return JSONObject.toJSONString(staffMerchantService.getStoreLargessSelect());
    }

    @RequestMapping(value = "/staffmerchant-list.html")
    public ModelAndView cardType() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("staffmerchant-list");
        return modelAndView;
    }

    @RequestMapping(value = "/staffmerchant-add.html")
    public ModelAndView addCardType() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("staffmerchant-add");
        return modelAndView;
    }
}
